<?php 
/* 	These code was generated using phpCIGen v 2.a (21/05/2012)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com,
    #CV. Trust Solution, jl. Saronojiwo 2/19 Surabaya, http://www.ts.co.id

	+ Module  		: t_komp Model
	+ Description	: For record model process back-end
	+ Filename 		: c_t_komp.php
 	+ creator 		: 
 	+ Created on 24/Jul/2012 10:52:52

*/

class M_t_komp extends CI_Model{

		//constructor
		function __construct() {
			parent::__construct();
		}
		function get_vendor($filter, $vendor_type, $start, $end,$branch_id,$project_id,$job_id){
		
				$sql = "call prepare_tablesplit";
			$this->db->query($sql);
			$sql = "SELECT vendor_id, vendor_name, m_div_id FROM m_vendor";
			$query = $this->db->query($sql);
			foreach($query->result()  as $result){
				$sql = "call split_string('".$result->m_div_id."',',',".$result->vendor_id.");";
				$this->db->query($sql);
			}
			
			
			$kuery_vendorid = "
				SELECT GROUP_CONCAT(t_komp.m_vendor_id SEPARATOR ',') as list_vendorid
					FROM t_komp
					WHERE
					t_komp.m_branch_id = ". $branch_id ."
					AND t_komp.m_job_id = ". $job_id ."
					AND t_komp.t_project_id = ". $project_id ."
					GROUP BY t_komp.m_branch_id,t_komp.m_job_id, t_komp.t_project_id
			";
			$result = $this->db->query($kuery_vendorid);
			if($result->num_rows() > 0){
				$row = $result->row();
				$list_vendorid = $row->list_vendorid;
			}else{
				$list_vendorid = "";
			}
			$query = "SELECT vendor_id, vendor_name, vendor_address
					FROM m_vendor LEFT JOIN splitResults ON vendor_id = split_id
					WHERE split_value='". $vendor_type ."'
					";
			if($list_vendorid !== ""){
				$query .= " AND vendor_id NOT IN (". $list_vendorid .")";
			}
			// For simple search
			if ($filter<>""){
				$query .=preg_match("/\bWHERE\b/i",$query)? " AND ":" WHERE ";
				$query .= " (vendor_name LIKE '%".addslashes($filter)."%' )";
			}
		
			$result = $this->db->query($query);
			$nbrows = $result->num_rows();
			$limit = $query." LIMIT ".$start.", ".$end;
			$result = $this->db->query($limit);
				
			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}
		
		function kompdet_list($master_id,$query,$start,$end,$task,$komp_id) {
			if ($task == 'detail' && $komp_id <> '' ){
			
			
				$query = "select div_name from t_komp 
					LEFT JOIN t_rapk
					ON t_komp.m_branch_id = t_rapk.m_branch_id AND
					t_komp.m_job_id =  t_rapk.m_job_id AND
					t_komp.t_project_id = t_rapk.t_project_id
					left JOIN m_div on m_div_id = div_id
					where komp_id = ".$komp_id;
				$result = $this->db->query($query);
				$row = $result->row();
				$divisi = $row->div_name;
			
			if ( $divisi == '503. Sub Kontraktor'){
			
				$query = "SELECT t_rapkdet_id as rapkdet_id, 
					kompdet_id, 
					t_kompdet.m_jobdet_id as jobdet_id, 
					jobdet_name, 
					kompdet_unit,
					kompdet_vol,
					kompdet_rapkvolume, 
					kompdet_rapkprice,
					kompdet_price 
					FROM t_kompdet  
					left join m_jobdet on  t_kompdet.m_jobdet_id =  m_jobdet.jobdet_id
					where t_komp_id = ".$komp_id." AND t_rapkdet_id = 0
					UNION
					SELECT * FROM ( SELECT t_rapkdet_id as rapkdet_id, 
					kompdet_id, 
					t_kompdet.m_jobdet_id as jobdet_id, 
					ifnull(CONCAT(REPEAT('--', COUNT(parent.rapkdet_id) - 1), jobdet_name),node.rapkdet_uraian) as jobdet_name, 
					kompdet_unit,
					kompdet_vol,
					kompdet_rapkvolume, 
					kompdet_rapkprice,
					kompdet_price 
					FROM t_kompdet  
					left join m_jobdet on  t_kompdet.m_jobdet_id =  m_jobdet.jobdet_id
					LEFT JOIN t_rapkdet AS node on t_kompdet.t_rapkdet_id = rapkdet_id
					JOIN t_rapkdet AS parent ON(node.rapkdet_lft BETWEEN parent.rapkdet_lft AND parent.rapkdet_rgt)
					where t_komp_id = ".$komp_id."
					GROUP BY node.rapkdet_id
					ORDER BY node.rapkdet_lft) k";
					
				}else{

						
				$query = "SELECT t_rapkdet_id as rapkdet_id, 
					kompdet_id, 
					t_kompdet.m_jobdet_id as jobdet_id, 
					jobdet_name, 
					kompdet_unit,
					kompdet_vol,
					kompdet_rapkvolume, 
					kompdet_rapkprice,
					kompdet_price 
					FROM t_kompdet  
					left join m_jobdet on  t_kompdet.m_jobdet_id =  m_jobdet.jobdet_id
					where t_komp_id = ".$komp_id." AND t_rapkdet_id = 0
					UNION
					SELECT t_rapkdet_id as rapkdet_id, 
					kompdet_id, 
					t_kompdet.m_jobdet_id as jobdet_id, 
					 jobdet_name,
					kompdet_unit,
					kompdet_vol,
					kompdet_rapkvolume, 
					kompdet_rapkprice,
					kompdet_price 
					FROM t_kompdet  
					left join m_jobdet on  t_kompdet.m_jobdet_id =  m_jobdet.jobdet_id
					LEFT JOIN t_rapkdet AS node on t_kompdet.t_rapkdet_id = rapkdet_id
					where t_komp_id = ".$komp_id;
				
				}
			}else{
			
				$query = "SELECT div_name as divisi FROM `t_rapk` 
				LEFT JOIN m_div ON m_div_id = div_id where rapk_id = ".$master_id;
				$result = $this->db->query($query);
				$row = $result->row();
				$divisi = $row->divisi;
			
			if ( $divisi != '503. Sub Kontraktor'){
			$query = "SELECT rapkdet_id, 0 AS kompdet_id, jobdet_id, jobdet_name, rapkdet_unit AS kompdet_unit, rapkdet_volume AS kompdet_rapkvolume, rapkdet_price AS kompdet_rapkprice,rapkdet_volume as kompdet_vol
					FROM t_rapk JOIN t_rapkdet ON(t_rapkdet.t_rapk_id = t_rapk.rapk_id)
					JOIN m_jobdet ON(m_jobdet.jobdet_id = t_rapkdet.m_jobdet_id)
					WHERE t_rapk.rapk_id=".$master_id;
			}else{
			$query = "
			SELECT * FROM (
			SELECT node.rapkdet_publish as nodepublish , parent.rapkdet_publish as parentpublish,node.rapkdet_volume as kompdet_vol , node.rapkdet_id, 0 AS kompdet_id, jobdet_id, IFNULL(CONCAT(REPEAT('--', COUNT(parent.rapkdet_id) - 1), jobdet_name),node.rapkdet_uraian) AS jobdet_name,
						node.rapkdet_unit AS kompdet_unit, node.rapkdet_volume AS kompdet_rapkvolume, node.rapkdet_price AS kompdet_rapkprice,
						(COUNT(parent.rapkdet_id) - 1) AS depth
					FROM t_rapkdet AS node JOIN t_rapkdet AS parent ON(node.rapkdet_lft BETWEEN parent.rapkdet_lft AND parent.rapkdet_rgt)
					LEFT JOIN m_jobdet ON(m_jobdet.jobdet_id = node.m_jobdet_id)
					WHERE node.t_rapk_id = ".$master_id." 
					GROUP BY node.rapkdet_id
					ORDER BY node.rapkdet_lft
					
					) t_all 
				WHERE 
				parentpublish = 1 AND
				nodepublish = 1
					"; 
			}
			
			}
			$result = $this->db->query($query);
			$nbrows = $result->num_rows();
			$limit = $query." LIMIT ".$start.",".$end;
			$result = $this->db->query($limit);
				
			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}
		
		function t_kompdet_grouptemp_truncate($by){
			$sql = "SELECT * FROM t_kompdet_grouptemp WHERE t_kompdet_grouptemp.group_created_by = '".$by."' OR t_kompdet_grouptemp.group_created_by = 0 ";
			$query = $this->db->query($sql);
			foreach($query->result() as $result){
				$sql_2 = "DELETE FROM t_kompdet_groupdettemp WHERE group_id = ".$result->group_id ;
				$query_2 = $this->db->query($sql_2);
			}
			$sql = "DELETE 
					FROM t_kompdet_grouptemp 
					WHERE t_kompdet_grouptemp.group_created_by = '".$by."' OR t_kompdet_grouptemp.group_created_by = 0 ";
			
			$this->db->query("ALTER TABLE t_kompdet_grouptemp AUTO_INCREMENT = 1");
			$this->db->query("ALTER TABLE t_kompdet_groupdettemp AUTO_INCREMENT = 1");
			
			$this->db->query($sql);
			if($this->db->affected_rows()){
				return '1';
			}else {
				return '0';
			} 
		}
		
		function kompdet_list_all($by){
			$query = "SELECT t_kompdet_groupdettemp.rapkdet_id, groupdettemp_id, jobdet_id, jobdet_name, kompdet_volume, rapkdet_volume, unit_id, unit_name, rapkdet_price, rapkdet_publish,kompdet_price
					FROM `t_kompdet_groupdettemp` JOIN t_kompdet_grouptemp ON t_kompdet_groupdettemp.group_id = t_kompdet_grouptemp.group_id
					LEFT JOIN m_jobdet ON(m_jobdet.jobdet_id = t_kompdet_groupdettemp.m_jobdet_id)
					LEFT JOIN m_unit ON(m_unit.unit_id = t_kompdet_groupdettemp.rapkdet_unit)
					WHERE group_created_by = '".$by."' ";
				
			$result = $this->db->query($query);
			$nbrows = $result->num_rows();
				
			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}
		
		function t_kompdet_grouptemp_getAll($by){
			$query = "SELECT group_id,group_new, group_uraian, group_publish, group_kompprice, grouprapk_table.*
					FROM t_kompdet_grouptemp
					LEFT JOIN (
						SELECT if ( sum(node.rapkdet_publish) > 0 , 0 , 1)  as group_only,if ( sum(node.rapkdet_publish) > 0 , '' , sum(node.rapkdet_price * node.rapkdet_volume  )) as group_rapkprice, parent.rapkdet_id as rapkdet_id FROM t_rapkdet as parent
						LEFT JOIN t_rapkdet as node  on node.rapkdet_lft > parent.rapkdet_lft AND node.rapkdet_rgt < parent.rapkdet_rgt
						group by parent.rapkdet_id 
					) grouprapk_table
					ON t_kompdet_grouptemp.rapkdet_id = grouprapk_table.rapkdet_id
					WHERE group_created_by = '".$by."'
					ORDER BY group_id DESC";
				
			$result = $this->db->query($query);
			$nbrows = $result->num_rows();
				
			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}
		
		function t_kompdet_groupdettemp_create($kompdet_volume,$kompdet_price,$group_id, $m_jobdet_id, $rapkdet_volume, $rapkdet_unit, $rapkdet_price, $rapkdet_publish){
			$var_kompdet_price = ($kompdet_price == null)?0:$kompdet_price;
			$data = array(
					"group_id"=>$group_id,
					"m_jobdet_id"=>$m_jobdet_id,
					"rapkdet_volume"=>$rapkdet_volume,
					"kompdet_volume"=>$kompdet_volume,
					"rapkdet_unit"=>$rapkdet_unit,
					"rapkdet_price"=>$rapkdet_price,
					"rapkdet_id"=>0,
					"rapkdet_publish"=>$rapkdet_publish,
					"kompdet_price"=>$var_kompdet_price
			);
			$this->db->insert('t_kompdet_groupdettemp', $data);
			if($this->db->affected_rows()){
				return '1';
			}else {
				return '0';
			}
		}
		
		
		function t_kompdet_groupdettemp_update($kompdet_volume,$kompdet_price,$groupdettemp_id,$group_id, $m_jobdet_id, $rapkdet_volume, $rapkdet_unit, $rapkdet_price, $rapkdet_publish){
		$var_kompdet_price = ($kompdet_price == null)?0:$kompdet_price;		
			$data = array(
					"group_id"=>$group_id,
					"m_jobdet_id"=>$m_jobdet_id,
					"rapkdet_volume"=>$rapkdet_volume,
					"kompdet_volume"=>$kompdet_volume,
					"rapkdet_unit"=>$rapkdet_unit,
					"rapkdet_price"=>$rapkdet_price,
					"rapkdet_publish"=>$rapkdet_publish,
					"kompdet_price"=>$var_kompdet_price
			);
			$this->db->where('groupdettemp_id',$groupdettemp_id);
			$this->db->update('t_kompdet_groupdettemp', $data);
			if($this->db->affected_rows()){
				return '1';
			}else {
				return '0';
			}
		}
		
				
		function t_kompdet_grouptemp_create($group_uraian, $group_publish,$group_kompprice, $by){
			$var_group_kompprice = ($group_kompprice == null)?0:$group_kompprice;
			$data = array(
					"group_uraian"=>$group_uraian,
					"group_publish"=>$group_publish,
					"group_kompprice"=>$var_group_kompprice,
					"group_new"=>1,
					"group_created_by"=>$by,
					"rapkdet_id"=>0
			);
			$this->db->insert('t_kompdet_grouptemp', $data);
			if($this->db->affected_rows()){
				return '1';
			}else {
				return '0';
			}
		}
		
		function t_kompdet_grouptemp_update($group_id,$group_uraian, $group_publish,$group_kompprice, $by){
			$var_group_kompprice = ($group_kompprice == null)?0:$group_kompprice;
			$data = array(
					"group_uraian"=>$group_uraian,
					"group_publish"=>$group_publish,
					"group_kompprice"=>$var_group_kompprice,
					"group_created_by"=>$by
			);
			$this->db->where('group_id',$group_id);
			$this->db->update('t_kompdet_grouptemp', $data);
			if($this->db->affected_rows()){
				return '1';
			}else {
				return '0';
			}
		}
		
		function t_kompdet_grouptemp_delete($group_id){
			$sqld = "DELETE t_kompdet_grouptemp, t_rapkdet_groupdettemp 
					FROM t_kompdet_grouptemp LEFT JOIN t_rapkdet_groupdettemp ON(t_rapkdet_groupdettemp.group_id = t_kompdet_grouptemp.group_id)
					WHERE t_kompdet_grouptemp.group_id = ".$group_id;
			$this->db->query($sqld);
			if($this->db->affected_rows()){
				return '1';
			}else {
				return '0';
			}
		}
		
		function t_kompdet_groupdettemp_getAll($group_id){
			$query = "SELECT  rapkdet_id, groupdettemp_id, jobdet_id, jobdet_name, rapkdet_volume,kompdet_volume, unit_id, unit_name, rapkdet_price, rapkdet_publish,kompdet_price
					FROM t_kompdet_groupdettemp JOIN m_jobdet ON(m_jobdet.jobdet_id = t_kompdet_groupdettemp.m_jobdet_id)
					LEFT JOIN m_unit ON(m_unit.unit_id = t_kompdet_groupdettemp.rapkdet_unit)
					WHERE t_kompdet_groupdettemp.group_id = ".$group_id;
		
			$result = $this->db->query($query);
			$nbrows = $result->num_rows();
		
			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}
		
		function t_komp_data_subkon($rapk_id,$by){
			// get group atau parent //
			$sql = "SELECT * FROM t_rapkdet where t_rapk_id = ".$rapk_id." AND rapkdet_rgt <> rapkdet_lft + 1 AND rapkdet_publish=1 ";
			$query = $this->db->query($sql);
			foreach($query->result() as $rows){
				$data = array(
					'group_uraian' => $rows->rapkdet_uraian,
					'group_publish' => $rows->rapkdet_publish,
					'group_new' => 0,
					'rapkdet_id' => $rows->rapkdet_id,
					'group_created_by' => $by
				);
				$this->db->insert('t_kompdet_grouptemp',$data);
				
				if($this->db->affected_rows()){
					$group_id = $this->db->insert_id();
					// get detail atau child //
					$sql = "SELECT * FROM t_rapkdet where  rapkdet_publish = 1 AND t_rapk_id = ".$rapk_id." AND rapkdet_lft > ".$rows->rapkdet_lft.
							" AND rapkdet_rgt < ".$rows->rapkdet_rgt;
					$query = $this->db->query($sql);
					foreach($query->result() as $row){
						$data = array(
							'group_id' => $group_id,
							'm_jobdet_id' => $row->m_jobdet_id,
							'rapkdet_volume' =>  $row->rapkdet_volume,
							'kompdet_volume' =>  $row->rapkdet_volume,
							'rapkdet_unit' => $row->rapkdet_unit,
							'rapkdet_price' => $row->rapkdet_price,
							'rapkdet_id' => $row->rapkdet_id,
							'rapkdet_publish' =>  $row->rapkdet_publish
						);
						
						$this->db->insert('t_kompdet_groupdettemp',$data);
					}
				}
			}
			return 1;
		}
		
		function t_komp_edit_subkon($rapk_id,$by){
			// get group atau parent //
			/* 
			$sql = "SELECT * FROM t_kompdet where t_komp_id = ".$rapk_id." AND kompdet_rgt <> kompdet_lft + 1  "; */
			$sql = "SELECT * FROM t_kompdet where t_komp_id = ".$rapk_id." AND kompdet_uraian is not null ";
			$query = $this->db->query($sql);
			foreach($query->result() as $rows){
				$group_new = ($rows->t_rapkdet_id == 0)? 0:1; // disini
				$data = array(
					'kompdet_id' => $rows->kompdet_id,
					'group_uraian' => $rows->kompdet_uraian,
					'group_kompprice' => $rows->kompdet_price,
					'group_publish' => 1,
					'group_new' => $group_new,
					'rapkdet_id' => $rows->t_rapkdet_id,
					'group_created_by' => $by
				);
				$this->db->insert('t_kompdet_grouptemp',$data);
				
				if($this->db->affected_rows()){
					$group_id = $this->db->insert_id();
					// get detail atau child //
					$sql = "SELECT * FROM t_kompdet where t_komp_id = ".$rapk_id." AND kompdet_lft > ".$rows->kompdet_lft.
							" AND kompdet_rgt < ".$rows->kompdet_rgt;
					$query = $this->db->query($sql);
					foreach($query->result() as $row){
						$data = array(
							'group_id' => $group_id,
							'm_jobdet_id' => $row->m_jobdet_id,
							'rapkdet_volume' =>  $row->kompdet_rapkvolume,
							'kompdet_volume' =>  $row->kompdet_vol,
							'rapkdet_unit' => $row->kompdet_unit,
							'rapkdet_price' => $row->kompdet_rapkprice,
							'kompdet_price' => $row->kompdet_price,
							'rapkdet_id' => $row->t_rapkdet_id,
							'kompdet_id' => $row->kompdet_id,
							'rapkdet_publish' =>  1
						);
						
						$this->db->insert('t_kompdet_groupdettemp',$data);
					}
				}
			}
			return 1;
		}
		
		function get_nosurat_byvendor($vendor,$nosurat) {
			if ($nosurat==""){
				return 0;
			}
			
			$query = "SELECT komp_id, m_vendor_id, komp_no
					FROM t_komp
					WHERE m_vendor_id=".$vendor." AND komp_no='".$nosurat."'";
			
			$result = $this->db->query($query);
			$nbrows = $result->num_rows();
			
			if($nbrows>0){
				return 0;
			} else {
				return 1;
			}
		}
		
		function get_jobdet_all($task,$master_id,$selected_id,$filter,$start,$end){
			if($filter<>""){
				$task="all";
			}
			
			$sql = "SELECT IFNULL(rapkdet_id, 0) AS rapkdet_id, jobdet_id, jobdet_name
				FROM m_jobdet LEFT JOIN t_rapkdet ON(t_rapkdet.m_jobdet_id = m_jobdet.jobdet_id)";
			
			if($task=='detail'){
				if (($master_id<>"") && ($master_id<>0)){
					$sql .=preg_match("/\bWHERE\b/i",$sql)? " AND ":" WHERE ";
					$sql .= " (t_rapkdet.t_rapk_id = ".$master_id." )";
				}
			
				$result = $this->db->query($sql);
				$nbrows = $result->num_rows();
			
				if($nbrows>0){
					foreach($result->result() as $row){
						$arr[] = $row;
					}
					$jsonresult = json_encode($arr);
					return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
				} else {
					return '({"total":"0", "results":""})';
				}
			
			}else if($task=='selected'){
				$selected_id_list = "";
				for($i = 0; $i < sizeof($selected_id); $i++){
					if($i>0){
						$selected_id_list .= ",";
					}
					$selected_id_list .= $selected_id[$i];
				}
			
				if($selected_id_list!=="")
				{
					//$selected_id=substr($selected_id,0,strlen($selected_id)-1);
					$sql.=(preg_match("/\bWHERE\b/i",$sql)?" AND ":" WHERE ")." jobdet_id IN(".$selected_id_list.")";
				}
			
				$result = $this->db->query($sql);
				$nbrows = $result->num_rows();
			
				if($nbrows>0){
					foreach($result->result() as $row){
						$arr[] = $row;
					}
					$jsonresult = json_encode($arr);
					return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
				} else {
					return '({"total":"0", "results":""})';
				}
			
			}else{
				if ($filter<>""){
					$sql .=preg_match("/\bWHERE\b/i",$sql)? " AND ":" WHERE ";
					$sql .= " (m_jobdet.jobdet_name LIKE '%".addslashes($filter)."%' )";
				}
			
				$result = $this->db->query($sql);
				$nbrows = $result->num_rows();
				if($start=="") $start=0;
				if($end=="") $end=15;
				$limit = $sql." LIMIT ".$start.",".$end;
				$result = $this->db->query($limit);
			
				if($nbrows>0){
					foreach($result->result() as $row){
						$arr[] = $row;
					}
					$jsonresult = json_encode($arr);
					return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
				} else {
					return '({"total":"0", "results":""})';
				}
			}
			
		}

		//function for get list record
		function t_komp_list($filter,$start,$end){
			$sql = "SELECT komp_id, t_rapk.m_div_id, div_name, branch_id, branch_name, project_code, project_name, job_id, job_name, vendor_id, vendor_name, komp_date, komp_no
					FROM t_komp JOIN t_kompdet ON(t_kompdet.t_komp_id = t_komp.komp_id)
					JOIN t_project ON(t_project.project_id = t_komp.t_project_id)
					JOIN m_job ON(m_job.job_id = t_komp.m_job_id)
					JOIN t_rapk ON t_komp.m_branch_id =  t_komp.m_branch_id AND  t_komp.m_job_id = t_rapk.m_job_id AND  t_komp.t_project_id =  t_rapk.t_project_id
					JOIN m_div ON(t_rapk.m_div_id= div_id) 
					JOIN m_vendor ON(m_vendor.vendor_id = t_komp.m_vendor_id)
				
					LEFT JOIN m_branch ON(m_branch.branch_id = t_project.m_branch_id)";
			// For simple search
			if ($filter<>""){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " (project_code LIKE '%".addslashes($filter)."%' OR project_name LIKE '%".addslashes($filter)."%' OR job_name LIKE '%".addslashes($filter)."%' OR vendor_name LIKE '%".addslashes($filter)."%' OR komp_no LIKE '%".addslashes($filter)."%')";
			}
			$sql = $sql." GROUP BY t_komp.komp_id";
			$result = $this->db->query($sql);
			$result_nbrows = $this->db->query($sql);
			$nbrows = $result_nbrows->num_rows();
			$limit = $sql." LIMIT ".$start.", ".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for create new record
		function t_komp_create($branch, $project, $job, $vendor, $komp_date, $komp_no, 
				$array_rapkdet_id, $array_kompdet_id, $array_jobdet_id, $array_kompdet_vol, $array_kompdet_rapkvolume, $array_kompdet_unit, 
				$array_kompdet_rapkprice, $array_kompdet_price){
			$data = array(
					"m_branch_id"=>$branch,
					"t_project_id"=>$project,
					"m_job_id"=>$job,
					"m_vendor_id"=>$vendor,
					"komp_date"=>$komp_date,
					"komp_no"=>$komp_no
					);
			$this->db->insert('t_komp', $data);
			if($this->db->affected_rows()){
				$komp_id = $this->db->insert_id();
				for($i = 0; $i < sizeof($array_jobdet_id); $i++){
					$kompdet_id = $array_kompdet_id[$i];
					$t_rapkdet_id = $array_rapkdet_id[$i];
					$m_jobdet_id = $array_jobdet_id[$i];
					$kompdet_unit = $array_kompdet_unit[$i];
					$kompdet_vol = $array_kompdet_vol[$i];
					$kompdet_rapkvolume = $array_kompdet_rapkvolume[$i];
					$kompdet_rapkprice = $array_kompdet_rapkprice[$i];
					$kompdet_price = $array_kompdet_price[$i];
						
					$data_detail = array(
							"t_komp_id"=>$komp_id,
							"t_rapkdet_id"=>$t_rapkdet_id,
							"m_jobdet_id"=>$m_jobdet_id,
							"kompdet_unit"=>$kompdet_unit,
							"kompdet_vol"=>$kompdet_vol,
							"kompdet_rapkvolume"=>$kompdet_rapkvolume,
							"kompdet_rapkprice"=>$kompdet_rapkprice,
							"kompdet_price"=>$kompdet_price
							);
					$this->db->insert('t_kompdet', $data_detail);
				}
				
				return '1';
			}else{
				return '0';
			}
		}

		function t_komp_create_group($branch, $project, $job, $vendor, $komp_date, $komp_no
				){
			$data = array(
					"m_branch_id"=>$branch,
					"t_project_id"=>$project,
					"m_job_id"=>$job,
					"m_vendor_id"=>$vendor,
					"komp_date"=>$komp_date,
					"komp_no"=>$komp_no
					);
			$this->db->insert('t_komp', $data);
			if($this->db->affected_rows()){
				$komp_id = $this->db->insert_id();
				$by = @$_SESSION[SESSION_USERID];
					$sql = "SELECT * FROM t_kompdet_grouptemp WHERE group_created_by like '".$by."'";
					$query = $this->db->query($sql);
					foreach ( $query->result() as $rows ){ 
					
						$sql = "SELECT IFNULL((MAX(kompdet_rgt)+1), 1) AS max_rgt
							FROM t_kompdet";
						$rs = $this->db->query($sql);
						$myLeft = $rs->row()->max_rgt;
						
						//1. Add Parent === Uraian
							
						$data = array(
								"t_komp_id"=>$komp_id,
								"kompdet_uraian"=>$rows->group_uraian,
								"kompdet_uraian"=>$rows->group_uraian, // tanyakan nanti key($rows)
								"kompdet_lft"=>$myLeft,
								"t_rapkdet_id"=>$rows->rapkdet_id,
								"kompdet_rgt"=>$myLeft+1
						);
								
						if ( $rows->group_kompprice != null && $rows->group_kompprice != 0  ) {
							$data["kompdet_price"] = $rows->group_kompprice;
							$data["kompdet_vol"] = 1;
						}else{
							$data["kompdet_price"] = 0;
							$data["kompdet_vol"] = 0;
						}
						
						$this->db->insert('t_kompdet', $data);
						$create_affected = $this->db->affected_rows();
						$id = $this->db->insert_id();
							
						
						//2. Add Child of Parent
						$sql = "SELECT * FROM t_kompdet_groupdettemp WHERE group_id = ".$rows->group_id;
						$query = $this->db->query($sql);
					
						foreach ($query->result() as $row){
								$sql = "SELECT kompdet_lft FROM t_kompdet WHERE kompdet_id = ".$id;
								$rs = $this->db->query($sql);
								$myLeft = $rs->row()->kompdet_lft;
								
								$sqlu = "UPDATE t_kompdet SET kompdet_rgt = kompdet_rgt + 2 WHERE kompdet_rgt > ".$myLeft;
								$this->db->query($sqlu);
								$sqlu = "UPDATE t_kompdet SET kompdet_lft = kompdet_lft + 2 WHERE kompdet_lft > ".$myLeft;
								$this->db->query($sqlu);
								
								$var_kompdet_price = ($row->kompdet_price == null)?0:$row->kompdet_price ;
								
								$data = array(
										"t_komp_id"=>$komp_id,
										"m_jobdet_id"=>$row->m_jobdet_id,
										"t_rapkdet_id"=>$row->rapkdet_id,
										"kompdet_vol"=>$row->kompdet_volume,
										"kompdet_rapkvolume"=>$row->rapkdet_volume,
										"kompdet_unit"=>$row->rapkdet_unit,
										"kompdet_rapkprice"=>$row->rapkdet_price,
										"kompdet_price"=>$var_kompdet_price,
										"kompdet_lft"=>$myLeft+1,
										"kompdet_rgt"=>$myLeft+2
								);
								$this->db->insert('t_kompdet', $data);
								$create_affected = $this->db->affected_rows();
									
							
						}
						
					}
				
				return '1';
			}else{
				return '0';
			}
		}
		
		//function for update record
		function t_komp_update($komp_id, $branch, $project, $job, $vendor, $komp_date, $komp_no, 
				$array_rapkdet_id, $array_kompdet_id, $array_jobdet_id, $array_kompdet_vol, $array_kompdet_unit, 
				$array_kompdet_rapkprice, $array_kompdet_price){
				
				$sql_notdelete = "";
			for($i = 0; $i < sizeof($array_jobdet_id); $i++){
				$kompdet_id = $array_kompdet_id[$i];
				$t_rapkdet_id = $array_rapkdet_id[$i];
				$m_jobdet_id = $array_jobdet_id[$i];
				$kompdet_unit = $array_kompdet_unit[$i];
				$kompdet_vol = $array_kompdet_vol[$i];
				$kompdet_rapkprice = $array_kompdet_rapkprice[$i];
				$kompdet_price = $array_kompdet_price[$i];
	 			if($kompdet_id == 0){ //diganti karena klo ada data yang di hapus tidak hilang
					//insert data
					$data_c = array(
							"t_komp_id"=>$komp_id,
							"t_rapkdet_id"=>$t_rapkdet_id,
							"m_jobdet_id"=>$m_jobdet_id,
							"kompdet_unit"=>$kompdet_unit,
							"kompdet_vol"=>$kompdet_vol,
							"kompdet_rapkprice"=>$kompdet_rapkprice,
							"kompdet_price"=>$kompdet_price
							);
					$this->db->insert('t_kompdet', $data_c);
					$id = $this->db->insert_id();
					
					$sql_notdelete .= " AND kompdet_id <> ".$id ;
				}else{
					$data_u = array(
							"kompdet_unit"=>$kompdet_unit,
							"kompdet_vol"=>$kompdet_vol,
							"kompdet_rapkprice"=>$kompdet_rapkprice,
							"kompdet_price"=>$kompdet_price
							);
					$this->db->where('kompdet_id', $kompdet_id);
					$this->db->update('t_kompdet', $data_u);
					
					$sql_notdelete .= " AND kompdet_id <> ".$kompdet_id ;
				} 
					
			}
		//	echo "t_komp_id = ".$komp_id.$sql_notdelete;
			$this->db->where("t_komp_id = ".$komp_id.$sql_notdelete);
			$this->db->delete('t_kompdet');
				
			return '1';
		}

		
		function t_komp_update_group($komp_id, $branch, $project, $job, $vendor, $komp_date, $komp_no){
			// pertama-tama dihapus dulu
			$sql = "DELETE FROM t_kompdet WHERE t_komp_id = ".$komp_id;
			$this->db->query($sql);
			// sekarang yang ada di temporary di masukan lagi
			
			$by = @$_SESSION[SESSION_USERID];
			$sql = "SELECT * FROM t_kompdet_grouptemp WHERE group_created_by like '".$by."'";
			$query = $this->db->query($sql);
			foreach ( $query->result() as $rows ){ 
				$sql = "SELECT IFNULL((MAX(kompdet_rgt)+1), 1) AS max_rgt
					FROM t_kompdet";
				$rs = $this->db->query($sql);
				$myLeft = $rs->row()->max_rgt;
				
				$data = array(
					"t_komp_id"=>$komp_id,
					"kompdet_id"=>$rows->kompdet_id,
					"t_rapkdet_id"=>$rows->rapkdet_id,
					"kompdet_uraian"=>$rows->group_uraian, // tanyakan nanti key($rows)
					"kompdet_lft"=>$myLeft,
					"kompdet_rgt"=>$myLeft+1
				);
				
						
				if ( $rows->group_kompprice != null && $rows->group_kompprice != 0  ) {
					$data["kompdet_price"] = $rows->group_kompprice;
					$data["kompdet_vol"] = 1;
				}else{
					$data["kompdet_price"] = 0;
					$data["kompdet_vol"] = 0;
				}
								
				$this->db->insert('t_kompdet', $data);
				$create_affected = $this->db->affected_rows();
				$id = $this->db->insert_id();
				
				$sql = "SELECT * FROM t_kompdet_groupdettemp WHERE group_id = ".$rows->group_id;
				$query = $this->db->query($sql);
					
				foreach ($query->result() as $row){
					$sql = "SELECT kompdet_lft FROM t_kompdet WHERE kompdet_id = ".$id;
					$rs = $this->db->query($sql);
					$myLeft = $rs->row()->kompdet_lft;
									
					$sqlu = "UPDATE t_kompdet SET kompdet_rgt = kompdet_rgt + 2 WHERE kompdet_rgt > ".$myLeft;
					$this->db->query($sqlu);
					$sqlu = "UPDATE t_kompdet SET kompdet_lft = kompdet_lft + 2 WHERE kompdet_lft > ".$myLeft;
					$this->db->query($sqlu);
								
					$data = array(
						"t_komp_id"=>$komp_id,
						"kompdet_id"=>$row->kompdet_id,
						"t_rapkdet_id"=>$row->rapkdet_id,
						"m_jobdet_id"=>$row->m_jobdet_id,
						"kompdet_vol"=>$row->kompdet_volume,
						"kompdet_rapkvolume"=>$row->rapkdet_volume,
						"kompdet_unit"=>$row->rapkdet_unit,
						"kompdet_rapkprice"=>$row->rapkdet_price,
						"kompdet_price"=>$row->kompdet_price,
						"kompdet_lft"=>$myLeft+1,
						"kompdet_rgt"=>$myLeft+2
					);
					$this->db->insert('t_kompdet', $data);
					$create_affected = $this->db->affected_rows();
				}
			}
			return 1;
		}
		
		//fcuntion for delete record
		function t_komp_delete($pkid){
			// You could do some checkups here and return '0' or other error consts.
			// Make a single query to delete all of the t_komps at the same time :
			if(sizeof($pkid)<1){
				return '0';
			} else if (sizeof($pkid) == 1){
				$sql = "SELECT t_komp.komp_id FROM t_nego 
						LEFT JOIN t_komp
						ON t_nego.m_branch_id = t_komp.m_branch_id 
						AND t_nego.m_job_id = t_komp.m_job_id
						AND t_nego.t_project_id  = t_komp.t_project_id 
						where t_komp.komp_id = ".$pkid[0];
				$query = $this->db->query($sql);
				if ($query->num_rows() == 0 ){
					$sql = "DELETE FROM t_komp WHERE komp_id = ".$pkid[0];
					$this->db->query($sql);
					return '1';
				}else{
					return '3';
				}
			} else {
			
				$sql_check = "SELECT t_komp.komp_id FROM t_nego 
						LEFT JOIN t_komp
						ON t_nego.m_branch_id = t_komp.m_branch_id 
						AND t_nego.m_job_id = t_komp.m_job_id
						AND t_nego.t_project_id  = t_komp.t_project_id 
							where  "; 
							
				$sql = "DELETE FROM t_komp WHERE ";
				for($i = 0; $i < sizeof($pkid); $i++){
					$sql = $sql . "komp_id = ".$pkid[$i];
					if($i<sizeof($pkid)-1){
						$sql = $sql . " OR ";
					}
					
					$sql_check = $sql_check . "komp_id= ".$pkid[$i];
					if($i<sizeof($pkid)-1){
						$sql_check = $sql_check . " OR ";
					}
				}
				$query = $this->db->query($sql_check);
				if ($query->num_rows() == 0 ){
					$this->db->query($sql);
					return '1';
				}else{
					return '3';
				}
			}
		}
	
		function t_kompdet_detailtemp_delete($pkid){
			// You could do some checkups here and return '0' or other error consts.
			// Make a single query to delete all of the t_rapks at the same time :
			if(sizeof($pkid)<1){
				return '0';
			} else if (sizeof($pkid) == 1){
				$sql = "DELETE FROM t_kompdet_groupdettemp WHERE groupdettemp_id = ".$pkid[0];
				$this->db->query($sql);
			} else {
				$sql = "DELETE FROM t_kompdet_groupdettemp WHERE ";
				for($i = 0; $i < sizeof($pkid); $i++){
					$sql = $sql . "groupdettemp_id = ".$pkid[$i];
					if($i<sizeof($pkid)-1){
						$sql = $sql . " OR ";
					}
				}
				$this->db->query($sql);
			}
			if($this->db->affected_rows()>0)
				return '1';
			else
				return '0';
		}
		
		//function for advanced search record
		function t_komp_search($komp_id,
				 $komp_flag,
				 $komp_code,
				 $komp_type,
				 $komp_name,
				 $komp_shortname,
				 $komp_pm,
				 $komp_semcm,
				 $komp_value,
				 $komp_start,
				 $komp_p1,
				 $komp_p2,
				 $komp_created_date,
				 $komp_created_by,
				 $komp_updated_date,
				 $komp_updated_by,
				 $komp_updated_count,
				 $m_branch,$start,$end){
			//full query
			$sql = "SELECT komp_id,
						   komp_flag,
						   komp_code,
						   komp_type,
						   komp_name,
						   komp_shortname,
						   komp_pm,
						   komp_semcm,
						   komp_value,
						   komp_start,
						   komp_p1,
						   komp_p2,
						   komp_created_date,
						   komp_created_by,
						   komp_updated_date,
						   komp_updated_by,
						   komp_updated_count,
						   m_branch_id
					FROM   t_komp";

			if($komp_id!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(komp_id) LIKE '%".addslashes(strtolower($komp_id))."%'";
			};
			if($komp_flag!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(komp_flag) LIKE '%".addslashes(strtolower($komp_flag))."%'";
			};
			if($komp_code!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(komp_code) LIKE '%".addslashes(strtolower($komp_code))."%'";
			};
			if($komp_type!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(komp_type) LIKE '%".addslashes(strtolower($komp_type))."%'";
			};
			if($komp_name!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(komp_name) LIKE '%".addslashes(strtolower($komp_name))."%'";
			};
			if($komp_shortname!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(komp_shortname) LIKE '%".addslashes(strtolower($komp_shortname))."%'";
			};
			if($komp_pm!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(komp_pm) LIKE '%".addslashes(strtolower($komp_pm))."%'";
			};
			if($komp_semcm!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(komp_semcm) LIKE '%".addslashes(strtolower($komp_semcm))."%'";
			};
			if($komp_value!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(komp_value) LIKE '%".addslashes(strtolower($komp_value))."%'";
			};
			if($komp_start!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(komp_start) LIKE '%".addslashes(strtolower($komp_start))."%'";
			};
			if($komp_p1!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(komp_p1) LIKE '%".addslashes(strtolower($komp_p1))."%'";
			};
			if($komp_p2!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(komp_p2) LIKE '%".addslashes(strtolower($komp_p2))."%'";
			};
			if($komp_created_date!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(komp_created_date) LIKE '%".addslashes(strtolower($komp_created_date))."%'";
			};
			if($komp_created_by!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(komp_created_by) LIKE '%".addslashes(strtolower($komp_created_by))."%'";
			};
			if($komp_updated_date!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(komp_updated_date) LIKE '%".addslashes(strtolower($komp_updated_date))."%'";
			};
			if($komp_updated_by!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(komp_updated_by) LIKE '%".addslashes(strtolower($komp_updated_by))."%'";
			};
			if($komp_updated_count!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(komp_updated_count) LIKE '%".addslashes(strtolower($komp_updated_count))."%'";
			};
			if($m_branch!=''){
				$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
				$sql.= " lower(m_branch_id) LIKE '%".addslashes(strtolower($m_branch))."%'";
			};

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
			$limit = $sql." LIMIT ".$start.",".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for print record
		function t_komp_print($komp_id,
				 $komp_flag,
				 $komp_code,
				 $komp_type,
				 $komp_name,
				 $komp_shortname,
				 $komp_pm,
				 $komp_semcm,
				 $komp_value,
				 $komp_start,
				 $komp_p1,
				 $komp_p2,
				 $komp_created_date,
				 $komp_created_by,
				 $komp_updated_date,
				 $komp_updated_by,
				 $komp_updated_count,
				 $m_branch,$option,$filter){
			//full query
			$sql = "SELECT komp_id,
						   komp_flag,
						   komp_code,
						   komp_type,
						   komp_name,
						   komp_shortname,
						   komp_pm,
						   komp_semcm,
						   komp_value,
						   komp_start,
						   komp_p1,
						   komp_p2,
						   komp_created_date,
						   komp_created_by,
						   komp_updated_date,
						   komp_updated_by,
						   komp_updated_count,
						   m_branch_id
				    FROM   t_komp";

			if($option=='LIST'){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(komp_id) LIKE '%".addslashes(strtolower($filter))."%' OR lower(komp_flag) LIKE '%".addslashes(strtolower($filter))."%' OR lower(komp_code) LIKE '%".addslashes(strtolower($filter))."%' OR lower(komp_type) LIKE '%".addslashes(strtolower($filter))."%' OR lower(komp_name) LIKE '%".addslashes(strtolower($filter))."%' OR lower(komp_shortname) LIKE '%".addslashes(strtolower($filter))."%' OR lower(komp_pm) LIKE '%".addslashes(strtolower($filter))."%' OR lower(komp_semcm) LIKE '%".addslashes(strtolower($filter))."%' OR lower(komp_value) LIKE '%".addslashes(strtolower($filter))."%' OR lower(komp_start) LIKE '%".addslashes(strtolower($filter))."%' OR lower(komp_p1) LIKE '%".addslashes(strtolower($filter))."%' OR lower(komp_p2) LIKE '%".addslashes(strtolower($filter))."%' OR lower(komp_created_date) LIKE '%".addslashes(strtolower($filter))."%' OR lower(komp_created_by) LIKE '%".addslashes(strtolower($filter))."%' OR lower(komp_updated_date) LIKE '%".addslashes(strtolower($filter))."%' OR lower(komp_updated_by) LIKE '%".addslashes(strtolower($filter))."%' OR lower(komp_updated_count) LIKE '%".addslashes(strtolower($filter))."%' OR lower(m_branch_id) LIKE '%".addslashes(strtolower($filter))."%' )";
			} else if($option=='SEARCH'){
				if($komp_id!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_id) LIKE '%".addslashes(strtolower($komp_id))."%'";
				};
				if($komp_flag!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_flag) LIKE '%".addslashes(strtolower($komp_flag))."%'";
				};
				if($komp_code!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_code) LIKE '%".addslashes(strtolower($komp_code))."%'";
				};
				if($komp_type!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_type) LIKE '%".addslashes(strtolower($komp_type))."%'";
				};
				if($komp_name!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_name) LIKE '%".addslashes(strtolower($komp_name))."%'";
				};
				if($komp_shortname!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_shortname) LIKE '%".addslashes(strtolower($komp_shortname))."%'";
				};
				if($komp_pm!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_pm) LIKE '%".addslashes(strtolower($komp_pm))."%'";
				};
				if($komp_semcm!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_semcm) LIKE '%".addslashes(strtolower($komp_semcm))."%'";
				};
				if($komp_value!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_value) LIKE '%".addslashes(strtolower($komp_value))."%'";
				};
				if($komp_start!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_start) LIKE '%".addslashes(strtolower($komp_start))."%'";
				};
				if($komp_p1!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_p1) LIKE '%".addslashes(strtolower($komp_p1))."%'";
				};
				if($komp_p2!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_p2) LIKE '%".addslashes(strtolower($komp_p2))."%'";
				};
				if($komp_created_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_created_date) LIKE '%".addslashes(strtolower($komp_created_date))."%'";
				};
				if($komp_created_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_created_by) LIKE '%".addslashes(strtolower($komp_created_by))."%'";
				};
				if($komp_updated_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_updated_date) LIKE '%".addslashes(strtolower($komp_updated_date))."%'";
				};
				if($komp_updated_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_updated_by) LIKE '%".addslashes(strtolower($komp_updated_by))."%'";
				};
				if($komp_updated_count!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_updated_count) LIKE '%".addslashes(strtolower($komp_updated_count))."%'";
				};
				if($m_branch!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(m_branch_id) LIKE '%".addslashes(strtolower($m_branch))."%'";
				};
			}
			$query = $this->db->query($sql);
			return $query->result();
		}

		//function  for export to excel
		function t_komp_export_excel($komp_id ,$komp_flag ,$komp_code ,$komp_type ,$komp_name ,$komp_shortname ,$komp_pm ,$komp_semcm ,$komp_value ,$komp_start ,$komp_p1 ,$komp_p2 ,$komp_created_date ,$komp_created_by ,$komp_updated_date ,$komp_updated_by ,$komp_updated_count ,$m_branch ,$option,$filter){
			//full query
			$sql = "SELECT komp_id,
						   komp_flag,
						   komp_code,
						   komp_type,
						   komp_name,
						   komp_shortname,
						   komp_pm,
						   komp_semcm,
						   komp_value,
						   komp_start,
						   komp_p1,
						   komp_p2,
						   komp_created_date,
						   komp_created_by,
						   komp_updated_date,
						   komp_updated_by,
						   komp_updated_count,
						   m_branch_id				    FROM   t_komp";

			if($option=='LIST'){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= "(lower(komp_id) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(komp_flag) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(komp_code) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(komp_type) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(komp_name) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(komp_shortname) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(komp_pm) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(komp_semcm) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(komp_value) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(komp_start) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(komp_p1) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(komp_p2) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(komp_created_date) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(komp_created_by) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(komp_updated_date) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(komp_updated_by) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(komp_updated_count) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(m_branch_id) LIKE '%".addslashes(strtolower($filter))."%' )";
			} else if($option=='SEARCH'){
				if($komp_id!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_id) LIKE '%".addslashes(strtolower($komp_id))."%'";
				};
				if($komp_flag!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_flag) LIKE '%".addslashes(strtolower($komp_flag))."%'";
				};
				if($komp_code!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_code) LIKE '%".addslashes(strtolower($komp_code))."%'";
				};
				if($komp_type!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_type) LIKE '%".addslashes(strtolower($komp_type))."%'";
				};
				if($komp_name!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_name) LIKE '%".addslashes(strtolower($komp_name))."%'";
				};
				if($komp_shortname!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_shortname) LIKE '%".addslashes(strtolower($komp_shortname))."%'";
				};
				if($komp_pm!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_pm) LIKE '%".addslashes(strtolower($komp_pm))."%'";
				};
				if($komp_semcm!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_semcm) LIKE '%".addslashes(strtolower($komp_semcm))."%'";
				};
				if($komp_value!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_value) LIKE '%".addslashes(strtolower($komp_value))."%'";
				};
				if($komp_start!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_start) LIKE '%".addslashes(strtolower($komp_start))."%'";
				};
				if($komp_p1!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_p1) LIKE '%".addslashes(strtolower($komp_p1))."%'";
				};
				if($komp_p2!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_p2) LIKE '%".addslashes(strtolower($komp_p2))."%'";
				};
				if($komp_created_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_created_date) LIKE '%".addslashes(strtolower($komp_created_date))."%'";
				};
				if($komp_created_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_created_by) LIKE '%".addslashes(strtolower($komp_created_by))."%'";
				};
				if($komp_updated_date!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_updated_date) LIKE '%".addslashes(strtolower($komp_updated_date))."%'";
				};
				if($komp_updated_by!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_updated_by) LIKE '%".addslashes(strtolower($komp_updated_by))."%'";
				};
				if($komp_updated_count!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(komp_updated_count) LIKE '%".addslashes(strtolower($komp_updated_count))."%'";
				};
				if($m_branch!=''){
					$sql.=preg_match("/WHERE/i",$sql)?" AND ":" WHERE ";
					$sql.= " lower(m_branch_id) LIKE '%".addslashes(strtolower($m_branch))."%'";
				};
			}
			$query = $this->db->query($sql);
			return $sql;
		}

}
?>